﻿using LaunchAllaround.DesignPattern.MVC;
using LaunchAllaround.Pad;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LaunchAllaround.Playlist
{
    /// <summary>
    /// Logique d'interaction pour PlaylistUI.xaml
    /// </summary>
    public partial class PlaylistUI : UserControl, View<PlaylistModel>, Controller<PlaylistModel>
    {

        private Model<PlaylistModel> model;

        public PlaylistUI()
        {
            this.model = new PlaylistModel(this);
            InitializeComponent();
        }

        public PlaylistUI(PlaylistModel model)
        {
            this.model = model;
            InitializeComponent();
        }

        public void Refresh(RefreshEvent<PlaylistModel> ev)
        {
            this.HideAllPads();
            int padViewIndex = 0;
            foreach (PadModel item in (PlaylistModel)ev.Src)
            {
                Grid padGrid = ((Grid)this.PlaylistGrid.Children[padViewIndex]);
                if (padViewIndex == 0)
                {
                   PadUI pui = (PadUI)( item.GetViews().Get(0) );
                   padGrid.Children.Clear();
                   padGrid.Children.Add(pui);
                }
                padGrid.Visibility = Visibility.Visible;
                padGrid.Background = new SolidColorBrush(item.Color);
                padViewIndex++;
	        }
        }

        public void HideAllPads()
        {
            foreach (Grid pad in this.PlaylistGrid.Children){
                pad.Visibility = Visibility.Hidden;
            }
        }

        public Model<PlaylistModel> GetModel()
        {
            return this.model;
        }

        public void SetModel(Model<PlaylistModel> m)
        {
            this.model = m;
        }

        private void PadMouseEnterEvent(object sender, MouseEventArgs e)
        {
            ((Grid)sender).Opacity = 1;
        }

        private void PadMouseLeaveEvent(object sender, MouseEventArgs e)
        {
            ((Grid)sender).Opacity = 0.6;
        }

        private void PadClickEvent(object sender, MouseButtonEventArgs e)
        {
            String name = ((Grid)e.Source).Name;
            char posChar = name[3];
            int pos = int.Parse(posChar.ToString());
            ((PlaylistModel)this.model).ShiftFrom(pos);
        }

    }
}
